package com.cloud.wms.ui.inside.presenter;

import com.cloud.core.base.BaseRxPresenter;
import com.cloud.core.utils.MessageCreator;
import com.cloud.wms.app.FineExApplication;
import com.cloud.wms.config.SpKey;
import com.cloud.wms.http.Params;
import com.cloud.wms.http.observer.LoadingObserver;
import com.cloud.wms.http.observer.ResponseWrapper;
import com.cloud.wms.tools.PageHelper;
import com.cloud.wms.tools.RxUtils;
import com.cloud.wms.ui.inside.contract.TransferUpContact;
import com.cloud.wms.ui.inside.entity.TransferCommodityEntity;
import com.cloud.wms.ui.inside.entity.TransferUpEntity;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import io.reactivex.Observable;
import io.reactivex.functions.Function;

/**
 * Created by FC on 2021/5/20.
 * Desc：
 */
public class TransferUpPresenter extends BaseRxPresenter<TransferUpContact.View> implements TransferUpContact.Presenter {
    public static final int CONFIRM_UP = 0x101;

    @Inject
    public TransferUpPresenter() {

    }

    /**
     * 获取移库暂存库位或者容器商品信息
     */
    @Override
    public void getCommodityMsg() {
        Params params = Params.newInstance(Params.Type.WAREHOUSE);
        FineExApplication.component().http().transferUpCommodityMsg(params)
                .map(new ResponseWrapper<>())
                .compose(mView.toLifecycle())
                .compose(RxUtils.threadSwitch())
                .subscribe(new LoadingObserver<List<TransferUpEntity>>(mView.provideContext()) {
                    @Override
                    public void onSuccess(List<TransferUpEntity> entityList) {
                        if (entityList != null && !entityList.isEmpty())
                            for (int i = 0; i < entityList.size(); i++) {
                                for (int j = 0; j < entityList.get(i).getCommodityList().size(); j++) {
                                    entityList.get(i).getCommodityList().get(j).setContainerID(entityList.get(i).getContainerID());
                                }
                            }
                        mView.onSuccess(MessageCreator.createMessage(entityList, PageHelper.LIST_NEW_DATA));
                    }

                    @Override
                    public void onError(String msg) {
                        mView.error(msg);
                    }
                });
    }

    /**
     * 确认上架
     *
     * @param posID
     * @param commodityEntity
     */
    @Override
    public void confirmUp(String posID, String amount, TransferCommodityEntity commodityEntity) {
        Params params = Params.newInstance(Params.Type.WAREHOUSE);
        params.put("posID", posID);
        params.put("memberID", commodityEntity.getMemberID());
        params.put("commodityID", commodityEntity.getCommodityID());
        params.put("productBatchID", commodityEntity.getProductBatchID());
        params.put("stockType", commodityEntity.getStockType());
        params.put("amount", amount);
        params.put("containerID", commodityEntity.getContainerID());
        FineExApplication.component().http().transferConfirmUp(params)
                .map(new ResponseWrapper<>())
                .compose(mView.toLifecycle())
                .compose(RxUtils.threadSwitch())
                .subscribe(new LoadingObserver<Object>(mView.provideContext()) {
                    @Override
                    public void onSuccess(Object object) {
                        mView.onSuccess(MessageCreator.createMessage(object, CONFIRM_UP));
                    }

                    @Override
                    public void onError(String msg) {
                        mView.error(msg);
                    }
                });
    }
}
